
from rest_framework import serializers

from Headlines.models import Article
from question.models import Label, Question, Reply
from recruitment.models import Enterprise
from users.models import User

class ArticlesimpleSerializerForList(serializers.ModelSerializer):
    class Meta:

        model = Article
        fields = '__all__'
class UserSerializersimple(serializers.ModelSerializer):
    articles = ArticlesimpleSerializerForList(many=True)
    class Meta:

        model = User
        fields = '__all__'
# #我的收藏企业
class EnterpriseSerializerSimple(serializers.ModelSerializer):

    class Meta:

        model = Enterprise
        fields = '__all__'
class UsersimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model=User
        fields=['id']
# # #我的收藏文章
class ArticleSerializerForList(serializers.ModelSerializer):
    user = UserSerializersimple()
    collected = serializers.BooleanField(default=False)
    collected_users=UsersimpleSerializer(many=True)
    class Meta:

        model = Article
        fields = '__all__'
class ReplySerializer(serializers.ModelSerializer):
    class Meta:
        model = Reply
        fields = '__all__'
# #我的回答
class ReplySerializerForSubAndParent(serializers.ModelSerializer):
    subs=ReplySerializer(many=True)
    user=UserSerializersimple()
    class Meta:
        model = Reply
        fields = '__all__'
#我的关注
class LabelsSerializer(serializers.ModelSerializer):
    class Meta:
        model = Label
        fields = '__all__'

# # # #我的提问
class QuestionsSerializer(serializers.ModelSerializer):
    labels=LabelsSerializer(many=True)
    class Meta:
        model = Question
        fields = '__all__'


class UserReplySerializer(serializers.ModelSerializer):
    labels = LabelsSerializer(required=False, many=True)
    questions=QuestionsSerializer(required=False,many=True)
    answer_question=ReplySerializerForSubAndParent(required=False,many=True)
    collected_articles=ArticleSerializerForList(required=False,many=True)
    enterpises=EnterpriseSerializerSimple(required=False,many=True)
    username=serializers.StringRelatedField(read_only=True)
    class Meta:
        model = User
        fields = ['id','username','mobile','realname','birthday','sex','avatar','website','city','email','address','labels','questions','answer_question',
                  'collected_articles','enterpises']


# 修改密码的
# class UserPwdSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = User
#         fields = '__all__'
#         extra_kwargs={
#             # 密码安全问题 不能读取
#             'password':{
#                 'write_only':True
#             }
#         }
#
#     def update(self, instance, validated_data):
#         # super继承父类方法  instance 修改的对象
#         instance = super().update(instance, validated_data)
#
#         instance.set_password(validated_data.get('password'))
#         # 修改完后 保存到数据库
#         instance.null()
# from recruitment.models import Enterprise
# from users.models import User
#
# class EnterpisesSerializer(serializers.ModelSerializer):
#     recruits = serializers.PrimaryKeyRelatedField(read_only=True, many=True)
#     class Meta:
#         model = Enterprise
#         fields = ('id','name','labels','logo','recruits','summary')
#
# class UserSerializer(serializers.ModelSerializer):
#     enterpises = EnterpisesSerializer()
#     class Meta:
#         model = User
#         fields = '__all__'
#         # 向视图返回 instance 对象
#         return instance



class ClollectUserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'
